<?php
/**
 * 《机器设备》实现类
 *
 * @author 孙晓晔
 * @version $Id: Equipment.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/Machine.class.php');

class Cnc extends Machine {
	
	public $type = '2';
	
	public $machine1 = array(
		'-' => '请选择',
		'1' => 'CNC',
		'2' => '车床',

		'9' => '其它',
	);
	
	public $machine2 = array(
		'-' => '请选择',
		'1' => '乔峰',
		'2' => '佑康',
		'3' => '国产',
		
		'4' => '宫野',
		'5' => '捷甬达',
		'6' => '斗山',
		'7' => '津上',
		'8' => '起亚',
		'9' => '钻攻',
		'10' => '龙泽',

		'99' => '其它',
	);

	public $orderby = array(
		'-' => '请选择',
		'name' => '简称',
		
		'mold' => '产品',
		'emp' => '负责人',
			
		'status' => '状态',
	
		'1' => '创建时间',
		'2' => '更新时间',
	);
	
	public $groupby = array(
		'-' => '请选择',
		'emp' => '负责人',
		'machine1' => '分类',
		'machine2' => '品牌',
			
		'status' => '状态',
		'2' => '日期',
		'3' => '月份',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);
		
		$controller->assign('pagesize_s', '50');

		$controller->display($request, 'car/machine/cnc/find');
		return true;
	}

	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];

		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);

			return $row;
		}

		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}

		$controller->assign('orderby', $orderby);

		return $row;
	}

	function _where($request) {
		$code_s = $this->_input($request['code_s']);
		
		$name_s = $request['name_s'];

		$machine1_s = $request['machine1_s'];
		$machine2_s = $request['machine2_s'];

		$where = " WHERE type = 2";

		if (!empty($code_s)) {
			$where .= " AND code = '" . $code_s . "'";
		}

		if (!empty($name_s)) {
			$where .= " AND name like '%" . $name_s . "%'";
		}
		
		if (!empty($machine1_s) && $machine1_s <> '-') {
			$where .= " AND machine1 = '" . $machine1_s . "'";
		}
		
		if (!empty($machine2_s) && $machine2_s <> '-') {
			$where .= " AND machine2 = '" . $machine2_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'car/machine/cnc/list');
		return true;
	}

	function xls($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);
		
		$request['tpl'] = "xls";

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);
		
		$result = $db->query($sql);
		$data = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);

			$row = $this->_orderby_x($controller, $row, $this->orderby_s);

			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);
		
		$filename = sprintf("%s.xls", date("YmdHis"));
		header('Content-Disposition:application/vnd.ms-excel; filename=' . $filename);

		$controller->display($request, 'info/machine/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function my($controller, $request) {

		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'car/machine/cnc/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$code = $request['code'];
		$name = $request['name'];
				
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);

		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$content = addslashes($request['content']);
		
		$machine1 = $request['machine1'];
		$machine2 = $request['machine2'];
		
		$type = '2';

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, emp, date, code, name, content, type, machine1, machine2, oper, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $code, $name, $content, $type, $machine1, $machine2, $_SESSION['id']);
		debug($sql);
		$db->query($sql);

		$id = $db->insert_id();

		$request['id_s'] = $id;
		
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/machine/cnc/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
		
		$date = $request['date'];
		
		$code = $request['code'];
		$name = $request['name'];
				
		$zjm = getPinyin($name);
		$zjm = strtoupper($zjm);

		$supplier = $this->_input($request['supplier']);
		$supplier_x = addslashes($request['supplier_x']);
		
		$content = addslashes($request['content']);
		
		$machine1 = $request['machine1'];
		$machine2 = $request['machine2'];

		$status = $request['status'];
		
		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', code = '%s', name = '%s', content = '%s'";
		$fmt .= ", machine1 = '%s', machine2 = '%s', status = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $code, $name, $content, $machine1, $machine2, $status, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/machine/cnc/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		
		$row = parent::_view($id, $row);
		
		$row['photo_t'] = $this->_photo($row);
		
		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function copy($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'car/machine/cnc/copy');
		return true;
	}
	
	function group($controller, $request) {
		$this->pretreat_group($controller);
	
		// $controller->assign('t1', strtotime('-1 month'));
	
		$num = array(
			'1' => '按次数',
			'2' => '按工时',
		);
		$controller->assign('num_option', $num);
	
		$controller->display($request, 'car/machine/cnc/group');
		return true;
	}
	
	function groupby($controller, $px) {
		$px = $this->groupby_aa($px, $this->groupby, 'machine1');
	
		$controller->assign('groupby', $this->groupby[$px]);
	
		return $this->groupby_dd($px);
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function groupPost($controller, $request) {
		$this->pretreat($controller);
			
		$this->groupPara($controller, $request);
	
		$num_s = $request['num_s'];
		$fmt = "SELECT %s AS a, COUNT(*) AS c FROM %s a ";
		$sql = sprintf($fmt, $this->s, $this->tbl);
	
		$where = $this->_where($request);
	
		$request['sql'] = sprintf("%s %s GROUP BY %s ORDER BY %s %s", $sql, $where, $this->s, $this->orderby, $this->desc);
	
		$this->_group($controller, $request);
	
		// $controller->display($request, 'list');
		return true;
	}
	
	function _name($row, $px) {
		if (empty($px) || $px == '-' || $px == 'machine1') {
			$row['a'] = $this->machine1[$row['a']];
		} elseif ($px == 'machine2') {
			$row['a'] = $this->machine2[$row['a']];
		} else {
	
			$row = $this->_name_t($row, $px);
		}
	
		return $row;
	}
	
	function _list($status = 'all') {
		
		return parent::_list('2', $status);
	}
	
	function ajax_b($controller, $request) {
		$id = $_COOKIE['id_'];
		$ret = array();
	
		if ($this->_ajax_x($id, 'emp')) {
			$ret['status'] = 1;
		} elseif ($this->_ajax_x($id, 'oper')) {
			$ret['status'] = 1;
		} elseif ($this->_audit('15')) {
			$ret['status'] = 1;
		} elseif ($this->_is_admin()) {
			$ret['status'] = 1;
		} else {
			$ret['status'] = 2;
		}
	
		echo json_encode($ret);
	}

	function z($controller, $request) {
		$this->pretreat($controller);
	
		$db = newdb($this->dsn_w);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$json = $this->json($row);
	
			$fmt = "UPDATE %s SET json = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $json, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	
		logdate($controller, $request);
		return true;
	}
	
	function json($data) {
		
		$a = array();
	
		$a['type'] = urlencode($data['type']);
		
		$a['category'] = urlencode($data['category']);
		$a['cpxh'] = urlencode($data['cpxh']);
		$a['nsr'] = urlencode($data['nsr']);
		$a['fdjh'] = urlencode($data['fdjh']);
		$a['cjh'] = urlencode($data['cjh']);
		$a['buydate'] = urlencode($data['buydate']);
		$a['name4s'] = urlencode($data['name4s']);
		
		$a['address4s'] = urlencode($data['address4s']);
		$a['park'] = urlencode($data['park']);
		
		return urldecode(json_encode($a));
		
	}

	function u($controller, $request) {
		logdate($controller, $request);
		return true;
	}
}
?>